
 /**
 * HibernateRoleDao.java.java.
 * User: BrianChen Date: 2012-6-8 下午03:03:51
 *
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.collapsar.core.security.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.collapsar.core.orm.hibernate.BaseHibernateDao;
import com.collapsar.core.security.dao.IRoleDao;
import com.collapsar.core.security.model.Role;

/**
 * <pre>
 * REVISIONS: 
 * Version 	Date 		Author 			Description
 * ------------------------------------------------------------------- 
 * 1.0 		2012-6-8 	BrianChen 	1. Created this class. 
 * </pre>
 * 
 * @author BrianChen
 * @date 2012-6-8 下午03:03:51
 * @version 1.0
 * @see
 * 
 */
public class HibernateRoleDao extends BaseHibernateDao implements IRoleDao{

	@Override
	public void storeRole(Role role) {
		this.store(role);
	}

	@Override
	public void deleteRole(Role role) {
		this.delete(role);
	}

	@Override
	public void deleteAllRoles(Long[] roleIds) {
		this.deleteAll(Role.class, roleIds);
	}

	@Override
	public Role loadRole(Long roleId) {
		return (Role)this.load(Role.class, roleId);
	}

//	@Override
//	public Role loadRoleWithUsers(Long roleId) {
//		Role role = this.loadRole(roleId);
//		this.initalize(role.getUsers());
//		return role;
//	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Role getRoleByName(final String roleName) {
		return (Role)this.getHibernateTemplate().execute(
				new HibernateCallback(){

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						return session.getNamedQuery("GetRoleByName")
						.setParameter("roleName", roleName)
						.uniqueResult();
					}
				});
	}

}

